package com.guochuang.yunwang.promanager.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
@Slf4j
public class LogAspect {
    @Pointcut("execution(public * com.guochuang.yunwang.promanager.controller.*.*(..))")
    public void pointCut(){}

    @Before("pointCut()")
    public void logBefore(JoinPoint joinPoint){
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        String requestURL = request.getRequestURL().toString();

        log.info("request url: {}", requestURL);
        log.info("In Method: {}", joinPoint.getSignature().getDeclaringTypeName());
        Object[] args = joinPoint.getArgs();
        log.info("args: {}", args);
    }

    @AfterReturning(value = "pointCut()", returning = "obj")
    public void logAfter(JoinPoint joinPoint, Object obj){
        log.info("After Method: {}", joinPoint.getSignature().getDeclaringTypeName());
        log.info("return: {}", obj);
    }

}
